﻿using Microsoft.Extensions.Logging;
using Prism.Commands;
using Prism.Modularity;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.ObjectModel;
using XeMail.Common.User;
using XeMail.Interfaces;
using XeMail.Models;

namespace XeMail.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        private string _title = "应用主页面";
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        //Region 管理对象
        private IRegionManager _regionManager;
        private IModuleCatalog _moduleCatalog;

        //private ObservableCollection<IModuleInfo> _modules;
        //private IModuleInfo _moduleInfo;

        private ObservableCollection<MainModel> _modules;
        private MainModel _moduleInfo;

        private DelegateCommand _loadModules;
        private DelegateCommand _loginCommand;

        //public IView View { get; set; }
        /// <summary>
        /// 依赖注入访问Service
        /// </summary>
        private readonly IMessageService _messageService;
        private readonly IDialogService _dialogService;
        private readonly IUser _myuser;
        private readonly ILogger _logger;

        public string MessageInfo { get; set; }

        public ObservableCollection<MainModel> Modules
        {
            get => _modules ?? (_modules = new ObservableCollection<MainModel>());
        }

        public DelegateCommand LoadModules { get => _loadModules = new DelegateCommand(InitModules); }

        public void InitModules()
        {
            _logger.LogInformation("InitModules-添加菜单模块。");
            var dirModuleCatalog = _moduleCatalog as DirectoryModuleCatalog;
            //Modules.AddRange(dirModuleCatalog.Modules);

            foreach (var module in dirModuleCatalog.Items)
            {
                var tempModule = module as ModuleInfo;
                switch (tempModule.ModuleName)
                {
                    case "Contact":
                        Modules.Add(new MainModel() 
                        { 
                            DisplayName = "联系人", 
                            Name = tempModule.ModuleName, 
                            IConPath = "/Views/contact_icon.png" 
                        });
                        break;
                    case "Schedule":
                        Modules.Add(new MainModel() 
                        { 
                            DisplayName = "规划", 
                            Name = tempModule.ModuleName, 
                            IConPath = "/Views/schedule_icon.png" 
                        });
                        break;
                }
            }
        }

        public MainModel ModuleInfo
        {
            get
            {
                return _moduleInfo;
            }
            set
            {
                _moduleInfo = value;
                Navigate(value);
            }
        }

        public MainWindowViewModel(IRegionManager regionManager, IModuleCatalog moduleCatalog, IMessageService messageService, IDialogService dialogService, IUser myuser, ILogger logger)
        {
            //throw new Exception("hello world.");
            _regionManager = regionManager;
            _moduleCatalog = moduleCatalog;

            //测试接口, 获取随机窗口名称
            _messageService = messageService;
            _dialogService = dialogService;
            _myuser = myuser;
            _logger = logger;

            logger.LogInformation("Ctor，读取模块信息。");
            this.Title = _messageService.GetMessage();
        }


        public DelegateCommand LoginCommand
        {
            get => _loginCommand ?? (_loginCommand = new DelegateCommand(LoginAtion));
        }

        private void LoginAtion()
        {
            _dialogService.ShowDialog("UserLoginView", (r) =>
            {
                var result = r.Result;
                if (result == ButtonResult.OK)
                {
                    var loginStatus = r.Parameters.GetValue<bool>("LoginStatus");
                    _myuser.SetUserLoginState(loginStatus);
                }
            });
        }


        private void Navigate(MainModel info)
        {
            _logger.LogInformation("读取区域模块。");
            _regionManager.RequestNavigate("ContentRegion", $"{info.Name}");
        }
    }
}
